package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;

public class SootooNewsAnalyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		// http://www.sootoo.com/content/365717.shtml
		boolean bRet=false;
		String[] regex={ "http://.*[.]sootoo[.]com/content/[0-9]*[.]shtml" };
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				bRet=true;
				break;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		if (!isDetailPage(urlMeta.getUrl())) {
		}
		String html=urlMeta.getHtml();
		if (html == null) {
		}
		String title="";
		String content="";
		String url = urlMeta.getUrl();
		Long date=null;
		String source="速途网";
		int commentNum=0;
		int clickNum=0;
		NewsMeta meta=new NewsMeta();
		meta.setUrl(urlMeta.getUrl());
		meta.setType(0);
		Document doc=Jsoup.parse(html);
		urlMeta=null;
		/*
		 * 解析新闻标题
		 */
		try {
			title=doc.select("div.t11_wz>div.t11_mlblk>div").get(0).child(1)
					.text();
		} catch (Exception e) {
			// TODO Auto-generated catch block
		}

		/*
		 * 解析时间
		 */
		date=DateUtils.matchDate(doc.select("div.t11_info").text());

		/*
		 * 解析内容
		 */
		content=HtmlCleaner.getContentHtml(url,doc.select("div#content"));

		String comment=doc.select("span[style=color:#9D1112;margin:5px;]")
				.text();
		String click=doc.select("span[style=color:#9D1112;]").text();
		try {
			commentNum=Integer.parseInt(comment);
			clickNum=Integer.parseInt(click);
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block

		}
		meta.setTitle(title);
		meta.setContent(content);
		meta.setDate(date);
		meta.setClickNum(clickNum);
		meta.setCommentNum(commentNum);

		/**
		 * 解析评论数
		 */
		meta.setUpdateUrl(meta.getUrl());

		String author=null;
		Elements select=doc.select("div.content div.t11_info div a");
		if(select!=null && select.size()!=0){
			author=select.get(0).text();
		}
		meta.setAuthor(author);
		return meta;
	}

	public static void main(String[] args) {
		String url="http://www.sootoo.com/content/365198.shtml";
		UrlMeta urlMeta=CrawlHTML.responseToURL(url);
		SootooNewsAnalyse cnhubeiNewsAnalyse=new SootooNewsAnalyse();
		boolean detailPage=cnhubeiNewsAnalyse.isDetailPage(url);
		if (detailPage) {
			NewsMeta parserHtml=cnhubeiNewsAnalyse.parserHtml(urlMeta);
			System.out.println(parserHtml);
		} else {
			System.out.println("不符合正则");
		}
	}

	
	public NewsMeta Update(NewsMeta meta) {
		if (meta != null) {
			String updateUrl=meta.getUpdateUrl();
			if (updateUrl != null) {
				UrlMeta responseToURL=CrawlHTML.responseToURL(updateUrl);
				if (responseToURL != null) {
					String html=responseToURL.getHtml();
					if (html != null) {
						Document doc=Jsoup.parse(html);
						String comment=doc.select(
								"span[style=color:#9D1112;margin:5px;]").text();
						String click=doc.select("span[style=color:#9D1112;]")
								.text();
						try {
							int commentNum=Integer.parseInt(comment);
							int clickNum=Integer.parseInt(click);
							meta.setCommentNum(commentNum);
							meta.setClickNum(clickNum);
						} catch (NumberFormatException e) {
							// TODO Auto-generated catch block

						}
						return meta;
					}
				}
			}
		}
		return null;
	}
	
	public boolean isNeedUpdate(){
		return true;
	}
}
